package common.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * Created by yangkuo02 on 2017/6/5.
 */

public class NetworkStatus {

    public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] infos = cm.getAllNetworkInfo();
        if (infos != null) {
            for (NetworkInfo info : infos) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isWifiAvailable(Context context) {
        return isAvailableByType(context, ConnectivityManager.TYPE_WIFI);
    }

    public static boolean isMobileAvailable(Context context) {
        return isAvailableByType(context, ConnectivityManager.TYPE_MOBILE);
    }

    public static boolean isAvailableByType(Context context, int type) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info != null && info.getType() == type) {
            return true;
        }
        return false;
    }
}
